package com.uzai.console.service.sop.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.sop.sopprirewardrulestrategy.SopPriRewardRuleStrategyDto;
import com.uzai.console.dto.sop.sopprirewardrulestrategy.SopPriRewardRuleStrategyIdDto;
import com.uzai.console.dto.sop.sopprirewardrulestrategy.SopPriRewardRuleStrategyQuery;
import com.uzai.console.entity.MarketingTag;
import com.uzai.console.entity.PyqDevicePlan;
import com.uzai.console.entity.SopPriRewardRuleStrategy;
import com.uzai.console.jsonvo.sop.SopPriRewardRuleStrategyData;
import com.uzai.console.mapper.MarketingTagMapper;
import com.uzai.console.mapper.SopPriRewardRuleStrategyMapper;
import com.uzai.console.service.sop.SopPriRewardRuleStrategyService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.sop.sopprirewardrulestrategy.SopPriRewardRuleStrategyVo;
import com.uzai.console.vo.user.SysTagShow;
import com.uzai.console.vo.user.SysTagVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * SOP任务
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class SopPriRewardRuleStrategyServiceImpl implements SopPriRewardRuleStrategyService {

    @Autowired
    private SopPriRewardRuleStrategyMapper sopPriRewardRuleStrategyMapper;
    @Autowired
    private MarketingTagMapper marketingTagMapper;

    /**
     * 查询SOP定向规则策略
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return SopPriRewardRuleStrategyVo
     */
    public Page<SopPriRewardRuleStrategyVo> findByPage(SopPriRewardRuleStrategyQuery sopPriRewardRuleStrategyQuery){

        //新建返回到页面的LIST对象
        List<SopPriRewardRuleStrategyVo> sopPriRewardRuleStrategyVoList = new ArrayList<>();

        //执行分页查询对象
        Page<SopPriRewardRuleStrategy> page = new Page<>();
        sopPriRewardRuleStrategyQuery.convert(page);
        List<SopPriRewardRuleStrategy> sopPriRewardRuleStrategyList = sopPriRewardRuleStrategyMapper.findByList(page, sopPriRewardRuleStrategyQuery);
        if(sopPriRewardRuleStrategyList != null){
            for (SopPriRewardRuleStrategy sopPriRewardRuleStrategy : sopPriRewardRuleStrategyList){
                SopPriRewardRuleStrategyVo sopPriRewardRuleStrategyVo = new SopPriRewardRuleStrategyVo();
                BeanUtils.copyProperties(sopPriRewardRuleStrategy, sopPriRewardRuleStrategyVo);

                if(StringUtils.isNotBlank(sopPriRewardRuleStrategy.getData())){
                    try {
                        SopPriRewardRuleStrategyData sopPriRewardRuleStrategyData = JSONObject.parseObject(sopPriRewardRuleStrategy.getData(), SopPriRewardRuleStrategyData.class);
                        if(sopPriRewardRuleStrategyData != null){
                            sopPriRewardRuleStrategyVo.setBusyTimeInterval(sopPriRewardRuleStrategyData.getBusyTimeInterval());
                            sopPriRewardRuleStrategyVo.setLimitTimeStart(sopPriRewardRuleStrategyData.getLimitTimeStart());
                            sopPriRewardRuleStrategyVo.setLimitTimeEnd(sopPriRewardRuleStrategyData.getLimitTimeEnd());
                            sopPriRewardRuleStrategyVo.setStrategyList(sopPriRewardRuleStrategyData.getStrategyList());
                            sopPriRewardRuleStrategyVo.setStrategyLimitList(sopPriRewardRuleStrategyData.getStrategyLimitList());
                            sopPriRewardRuleStrategyVo.setSysTagList(sopPriRewardRuleStrategyVo.getSysTagList());
                            sopPriRewardRuleStrategyVo.setSysTagsExcludeList(sopPriRewardRuleStrategyVo.getSysTagsExcludeList());
                            //会员标签
                            List<SysTagVo> sysTagVoList =  sopPriRewardRuleStrategyData.getSysTagList();
                            if(sysTagVoList != null && sysTagVoList.size() > 0){
                                List<SysTagShow> sysTagShowList = new ArrayList<>();
                                List<Long> sysTagsArr = new ArrayList<>();
                                for (SysTagVo sysTagVo : sysTagVoList) {
                                    sysTagsArr.add(sysTagVo.getId());
                                    SysTagShow sysTagShow = new SysTagShow();
                                    BeanUtils.copyProperties(sysTagVo, sysTagShow);
                                    //查询标签名称
                                    MarketingTag marketingTag = marketingTagMapper.selectById(sysTagVo.getId(), sopPriRewardRuleStrategy.getMerId());
                                    if(marketingTag != null){
                                        sysTagShow.setName(marketingTag.getName());
                                    }
                                    sysTagShowList.add(sysTagShow);
                                }
                                sopPriRewardRuleStrategyVo.setSysTagsArr(sysTagsArr);
                                sopPriRewardRuleStrategyVo.setSysTagVoArr(sysTagShowList);
                            }

                            //排除会员标签
                            List<SysTagVo> sysTagsExcludeList =  sopPriRewardRuleStrategyData.getSysTagsExcludeList();
                            if(sysTagsExcludeList != null && sysTagsExcludeList.size() > 0){
                                List<SysTagShow> sysTagVoExcludeArr = new ArrayList<>();
                                List<Long> sysTagsExcludeArr = new ArrayList<>();
                                for (SysTagVo sysTagExcludeVo : sysTagsExcludeList) {
                                    sysTagsExcludeArr.add(sysTagExcludeVo.getId());
                                    SysTagShow sysTagExcludeShow = new SysTagShow();
                                    BeanUtils.copyProperties(sysTagExcludeVo, sysTagExcludeShow);
                                    //查询标签名称
                                    MarketingTag marketingTagExclude = marketingTagMapper.selectById(sysTagExcludeVo.getId(), sopPriRewardRuleStrategy.getMerId());
                                    if(marketingTagExclude != null){
                                        sysTagExcludeShow.setName(marketingTagExclude.getName());
                                    }
                                    sysTagVoExcludeArr.add(sysTagExcludeShow);
                                }
                                sopPriRewardRuleStrategyVo.setSysTagsExcludeArr(sysTagsExcludeArr);
                                sopPriRewardRuleStrategyVo.setSysTagVoExcludeArr(sysTagVoExcludeArr);
                            }
                        }

                    }catch (Exception e){ //如果不是JSON格式

                    }
                }

                sopPriRewardRuleStrategyVoList.add(sopPriRewardRuleStrategyVo);
            }
        }
        //返回到页面分页对象
        Page<SopPriRewardRuleStrategyVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(sopPriRewardRuleStrategyVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询SOP定向规则策略
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param sopPriRewardRuleStrategyIdDto
     * @return null
     */
    public SopPriRewardRuleStrategyVo findById(SopPriRewardRuleStrategyIdDto sopPriRewardRuleStrategyIdDto){
        SopPriRewardRuleStrategyVo sopPriRewardRuleStrategyVo = new SopPriRewardRuleStrategyVo();
        if(sopPriRewardRuleStrategyIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }
        SopPriRewardRuleStrategy sopPriRewardRuleStrategy = sopPriRewardRuleStrategyMapper.selectById(sopPriRewardRuleStrategyIdDto.getId(), sopPriRewardRuleStrategyIdDto.getMerId());
        if(sopPriRewardRuleStrategy != null){
            BeanUtils.copyProperties(sopPriRewardRuleStrategy, sopPriRewardRuleStrategyVo);

            if(StringUtils.isNotBlank(sopPriRewardRuleStrategy.getData())){
                try {
                    SopPriRewardRuleStrategyData sopPriRewardRuleStrategyData = JSONObject.parseObject(sopPriRewardRuleStrategy.getData(), SopPriRewardRuleStrategyData.class);
                    if(sopPriRewardRuleStrategyData != null){
                        sopPriRewardRuleStrategyVo.setBusyTimeInterval(sopPriRewardRuleStrategyData.getBusyTimeInterval());
                        sopPriRewardRuleStrategyVo.setLimitTimeStart(sopPriRewardRuleStrategyData.getLimitTimeStart());
                        sopPriRewardRuleStrategyVo.setLimitTimeEnd(sopPriRewardRuleStrategyData.getLimitTimeEnd());
                        sopPriRewardRuleStrategyVo.setStrategyList(sopPriRewardRuleStrategyData.getStrategyList());
                        sopPriRewardRuleStrategyVo.setStrategyLimitList(sopPriRewardRuleStrategyData.getStrategyLimitList());
                        sopPriRewardRuleStrategyVo.setSysTagList(sopPriRewardRuleStrategyVo.getSysTagList());
                        sopPriRewardRuleStrategyVo.setSysTagsExcludeList(sopPriRewardRuleStrategyVo.getSysTagsExcludeList());
                        //会员标签
                        List<SysTagVo> sysTagVoList =  sopPriRewardRuleStrategyData.getSysTagList();
                        if(sysTagVoList != null && sysTagVoList.size() > 0){
                            List<SysTagShow> sysTagShowList = new ArrayList<>();
                            List<Long> sysTagsArr = new ArrayList<>();
                            for (SysTagVo sysTagVo : sysTagVoList) {
                                sysTagsArr.add(sysTagVo.getId());
                                SysTagShow sysTagShow = new SysTagShow();
                                BeanUtils.copyProperties(sysTagVo, sysTagShow);
                                //查询标签名称
                                MarketingTag marketingTag = marketingTagMapper.selectById(sysTagVo.getId(), sopPriRewardRuleStrategy.getMerId());
                                if(marketingTag != null){
                                    sysTagShow.setName(marketingTag.getName());
                                }
                                sysTagShowList.add(sysTagShow);
                            }
                            sopPriRewardRuleStrategyVo.setSysTagsArr(sysTagsArr);
                            sopPriRewardRuleStrategyVo.setSysTagVoArr(sysTagShowList);
                        }

                        //排除会员标签
                        List<SysTagVo> sysTagsExcludeList =  sopPriRewardRuleStrategyData.getSysTagsExcludeList();
                        if(sysTagsExcludeList != null && sysTagsExcludeList.size() > 0){
                            List<SysTagShow> sysTagVoExcludeArr = new ArrayList<>();
                            List<Long> sysTagsExcludeArr = new ArrayList<>();
                            for (SysTagVo sysTagExcludeVo : sysTagsExcludeList) {
                                sysTagsExcludeArr.add(sysTagExcludeVo.getId());
                                SysTagShow sysTagExcludeShow = new SysTagShow();
                                BeanUtils.copyProperties(sysTagExcludeVo, sysTagExcludeShow);
                                //查询标签名称
                                MarketingTag marketingTagExclude = marketingTagMapper.selectById(sysTagExcludeVo.getId(), sopPriRewardRuleStrategy.getMerId());
                                if(marketingTagExclude != null){
                                    sysTagExcludeShow.setName(marketingTagExclude.getName());
                                }
                                sysTagVoExcludeArr.add(sysTagExcludeShow);
                            }
                            sopPriRewardRuleStrategyVo.setSysTagsExcludeArr(sysTagsExcludeArr);
                            sopPriRewardRuleStrategyVo.setSysTagVoExcludeArr(sysTagVoExcludeArr);
                        }
                    }

                }catch (Exception e){ //如果不是JSON格式

                }
            }
        }
        return sopPriRewardRuleStrategyVo;
    }

    /**
     * 添加SOP定向规则策略
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param sopPriRewardRuleStrategyDto
     * @return null
     */
    public void addSopPriRewardRuleStrategy(SopPriRewardRuleStrategyDto sopPriRewardRuleStrategyDto){
        if(StringUtils.isBlank(sopPriRewardRuleStrategyDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入名称");
        }

        SopPriRewardRuleStrategyData sopPriRewardRuleStrategyData = new SopPriRewardRuleStrategyData();
        BeanUtils.copyProperties(sopPriRewardRuleStrategyDto, sopPriRewardRuleStrategyData);
        sopPriRewardRuleStrategyData.setBusyTimeInterval(sopPriRewardRuleStrategyDto.getBusyTimeInterval());
        sopPriRewardRuleStrategyData.setLimitTimeEnd(sopPriRewardRuleStrategyDto.getLimitTimeEnd());
        sopPriRewardRuleStrategyData.setLimitTimeStart(sopPriRewardRuleStrategyDto.getLimitTimeStart());
        sopPriRewardRuleStrategyData.setStrategyList(sopPriRewardRuleStrategyDto.getStrategyList());
        sopPriRewardRuleStrategyData.setStrategyLimitList(sopPriRewardRuleStrategyDto.getStrategyLimitList());
        sopPriRewardRuleStrategyData.setSysTagList(sopPriRewardRuleStrategyDto.getSysTagList());
        sopPriRewardRuleStrategyData.setSysTagsExcludeList(sopPriRewardRuleStrategyDto.getSysTagsExcludeList());

        int now =  DateUtil.getNowTime();
        SopPriRewardRuleStrategy sopPriRewardRuleStrategy = new SopPriRewardRuleStrategy();
        sopPriRewardRuleStrategy.setId(IdWorker.getId());
        sopPriRewardRuleStrategy.setMerId(sopPriRewardRuleStrategyDto.getMerId());
        sopPriRewardRuleStrategy.setName(sopPriRewardRuleStrategyDto.getName());
        sopPriRewardRuleStrategy.setData(JSONObject.toJSONString(sopPriRewardRuleStrategyData));
        sopPriRewardRuleStrategy.setCreateTime(now);
        sopPriRewardRuleStrategy.setUpdateTime(now);

        sopPriRewardRuleStrategyMapper.insert(sopPriRewardRuleStrategy);

    }

    /**
     * 删除SOP定向规则策略
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param sopPriRewardRuleStrategyIdDto
     * @return null
     */
    public void deleteSopPriRewardRuleStrategy(SopPriRewardRuleStrategyIdDto sopPriRewardRuleStrategyIdDto){
        if(sopPriRewardRuleStrategyIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        SopPriRewardRuleStrategy sopPriRewardRuleStrategy = sopPriRewardRuleStrategyMapper.selectById(sopPriRewardRuleStrategyIdDto.getId(), sopPriRewardRuleStrategyIdDto.getMerId());

        if(sopPriRewardRuleStrategy == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        //删除进粉渠道
        sopPriRewardRuleStrategyMapper.deleteById(sopPriRewardRuleStrategyIdDto.getId(), sopPriRewardRuleStrategyIdDto.getMerId());

    }


    /**
     * 修改SOP定向规则策略
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void updateSopPriRewardRuleStrategy(SopPriRewardRuleStrategyDto sopPriRewardRuleStrategyDto){

        if(sopPriRewardRuleStrategyDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        SopPriRewardRuleStrategy sopPriRewardRuleStrategy = sopPriRewardRuleStrategyMapper.selectById(sopPriRewardRuleStrategyDto.getId(), sopPriRewardRuleStrategyDto.getMerId());
        if(sopPriRewardRuleStrategy == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请进粉渠道不存在");
        }

        if(StringUtils.isBlank(sopPriRewardRuleStrategyDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入渠道名称");
        }


        SopPriRewardRuleStrategyData sopPriRewardRuleStrategyData = new SopPriRewardRuleStrategyData();
        BeanUtils.copyProperties(sopPriRewardRuleStrategyDto, sopPriRewardRuleStrategyData);
        sopPriRewardRuleStrategyData.setBusyTimeInterval(sopPriRewardRuleStrategyDto.getBusyTimeInterval());
        sopPriRewardRuleStrategyData.setLimitTimeEnd(sopPriRewardRuleStrategyDto.getLimitTimeEnd());
        sopPriRewardRuleStrategyData.setLimitTimeStart(sopPriRewardRuleStrategyDto.getLimitTimeStart());
        sopPriRewardRuleStrategyData.setStrategyList(sopPriRewardRuleStrategyDto.getStrategyList());
        sopPriRewardRuleStrategyData.setStrategyLimitList(sopPriRewardRuleStrategyDto.getStrategyLimitList());
        sopPriRewardRuleStrategyData.setSysTagList(sopPriRewardRuleStrategyDto.getSysTagList());
        sopPriRewardRuleStrategyData.setSysTagsExcludeList(sopPriRewardRuleStrategyDto.getSysTagsExcludeList());


        int now =  DateUtil.getNowTime();
        sopPriRewardRuleStrategy.setName(sopPriRewardRuleStrategyDto.getName());
        sopPriRewardRuleStrategy.setData(JSONObject.toJSONString(sopPriRewardRuleStrategyData));
        sopPriRewardRuleStrategy.setUpdateTime(now);
        sopPriRewardRuleStrategyMapper.updateById(sopPriRewardRuleStrategy);

    }

    /**
     * 查询修改SOP定向规则策略下拉列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param sopPriRewardRuleStrategyQuery
     * @return null
     */
    public List<SelectKeyVo> findSelectList(SopPriRewardRuleStrategyQuery sopPriRewardRuleStrategyQuery){
        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        List<SopPriRewardRuleStrategy> sopPriRewardRuleStrategyList =  sopPriRewardRuleStrategyMapper.findByList(sopPriRewardRuleStrategyQuery);
        if(sopPriRewardRuleStrategyList != null && sopPriRewardRuleStrategyList.size() > 0){
            for (SopPriRewardRuleStrategy sopPriRewardRuleStrategy : sopPriRewardRuleStrategyList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(sopPriRewardRuleStrategy.getId()));
                selectKeyVo.setValue(sopPriRewardRuleStrategy.getName());
                selectKeyVoList.add(selectKeyVo);
            }
        }
        return selectKeyVoList;
    }

}
